package com.imay.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.model.RankingGameModel;
import com.imay.live.model.UserModel;
import com.imay.live.utils.GlideUtil;
import com.imay.live.utils.ViewUtil;

import java.util.List;

/**
 * Created by chan on 2017/7/26 0026.
 */

public class GameRankingAdapter extends BaseQuickAdapter<List<RankingGameModel>, BaseViewHolder> {

    private LayoutInflater inflater;

    public GameRankingAdapter(Context context, List<List<RankingGameModel>> data) {
        super(R.layout.item_ranking_game_group, data);
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected void convert(BaseViewHolder helper, final List<RankingGameModel> items) {
        LinearLayout llRankingGame = helper.getView(R.id.ll_ranking_game);
        Context context = helper.getConvertView().getContext();

        //分割线
        int color = context.getResources().getColor(R.color.bg_f5);
        int divider = (int) context.getResources().getDimension(R.dimen.divider_size_1x);
        LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, divider);

        //
        int height = (int) context.getResources().getDimension(R.dimen.item_ranking_height);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
        if (items != null) {
            if (items.isEmpty()) {
                llRankingGame.setVisibility(View.GONE);
            } else {
                llRankingGame.setVisibility(View.VISIBLE);
                for (int i = 0; i < items.size(); i++) {
                    final RankingGameModel item = items.get(i);
                    View itemView = inflater.inflate(R.layout.item_ranking_game, null);
                    TextView game_title = ViewUtil.findView(itemView, R.id.item_ranking_game_title);
                    // 冠军
                    long guanUid = 0;
                    RelativeLayout rl_ranking_guanjun = ViewUtil.findView(itemView, R.id.rl_item_ranking_guanjun);
                    ImageView ranking_guanjun = ViewUtil.findView(itemView, R.id.iv_item_ranking_guanjun);
                    // 亚军
                    long yaUid = 0;
                    RelativeLayout rl_ranking_yajun = ViewUtil.findView(itemView, R.id.rl_item_ranking_yajun);
                    ImageView ranking_yajun_touxiang = ViewUtil.findView(itemView, R.id.iv_item_ranking_yajun_touxiang);
                    // 季军
                    long jiUid = 0;
                    RelativeLayout rl_ranking_jijun = ViewUtil.findView(itemView, R.id.rl_item_ranking_jijun);
                    ImageView ranking_jijun_touxiang = ViewUtil.findView(itemView, R.id.iv_item_ranking_jijun_touxiang);
                    // 箭头
                    ImageView ranking_jiantou = ViewUtil.findView(itemView, R.id.iv_item_ranking_jiantou);

                    game_title.setText(item.getRankName());
                    if (item.getUsers() == null) {
                        ranking_jiantou.setVisibility(View.INVISIBLE);
                        llRankingGame.addView(itemView, params);
                        continue;
                    }
                    for (RankingGameModel.UserRankMode userRankMode : item.getUsers()) {
                        UserModel userModel = userRankMode.getUser();
                        if (userRankMode.getRank() == 0) {
                            rl_ranking_guanjun.setVisibility(View.VISIBLE);
                            GlideUtil.loadCircleBorderImage(ranking_guanjun, userModel.getImgHead(), R.dimen.size_3dp, R.color.border_f3, R.mipmap.pnl_00_head108);
                            guanUid = userModel.getUid();
                        } else if (userRankMode.getRank() == 1) {
                            rl_ranking_yajun.setVisibility(View.VISIBLE);
                            GlideUtil.loadCircleBorderImage(ranking_yajun_touxiang, userModel.getImgHead(), R.dimen.size_3dp, R.color.border_d4, R.mipmap.pnl_00_head108);
                            yaUid = userModel.getUid();
                        } else if (userRankMode.getRank() == 2) {
                            rl_ranking_jijun.setVisibility(View.VISIBLE);
                            GlideUtil.loadCircleBorderImage(ranking_jijun_touxiang, userModel.getImgHead(), R.dimen.size_3dp, R.color.border_d8, R.mipmap.pnl_00_head108);
                            jiUid = userModel.getUid();
                        }
                    }

                    llRankingGame.addView(itemView, params);
                    if (i < items.size()) {
                        View view = new View(helper.getConvertView().getContext());
                        view.setBackgroundColor(color);
                        llRankingGame.addView(view, dividerParams);
                    }

                    if (item.getUsers().size() > 2) {
                        ranking_jiantou.setVisibility(View.VISIBLE);
                    } else {
                        ranking_jiantou.setVisibility(View.INVISIBLE);
                    }

                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (itemGameRankClick != null) {
                                if (item.getUsers().size() > 2) {
                                    itemGameRankClick.itemClick(item.getRankType(), item.getRankName());
                                }
                            }
                        }
                    });

                    final long finalGuanUid = guanUid;
                    rl_ranking_guanjun.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (itemGameRankClick != null) {
                                itemGameRankClick.personClick(finalGuanUid);
                            }
                        }
                    });

                    final long finalYaUid = yaUid;
                    rl_ranking_yajun.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (itemGameRankClick != null) {
                                itemGameRankClick.personClick(finalYaUid);
                            }
                        }
                    });

                    final long finalJiUid = jiUid;
                    rl_ranking_jijun.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (itemGameRankClick != null) {
                                itemGameRankClick.personClick(finalJiUid);
                            }
                        }
                    });
                }

            }
        }
    }

    private OnItemGameRankClick itemGameRankClick;

    public void setOnItemGameRankClick(OnItemGameRankClick itemGameRankClick) {
        this.itemGameRankClick = itemGameRankClick;
    }

    public interface OnItemGameRankClick {
        void itemClick(String rankType, String rankTitle);

        void personClick(long uid);
    }
}
